package com.navinfo.collect.activity

import SystemUtil
import android.annotation.SuppressLint
import android.content.DialogInterface
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.os.Message
import android.widget.Toast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.navinfo.collect.library.map.flutter.plugin.FlutterMapViewFlutterPlugin
import com.navinfo.collect.library.data.dao.impl.MapLifeDataBase
import com.navinfo.collect.library.map.source.RealmDBTileSource
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import org.oscim.layers.tile.vector.VectorTileLayer

@SuppressLint("Registered")
class FlutterMainActivity : FlutterActivity() {
    private lateinit var db: MapLifeDataBase

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        FlutterMapViewFlutterPlugin.registerWith(flutterEngine, this);
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        requestedOrientation = if (SystemUtil.isPad(this)) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE//竖屏
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT//竖屏
        }
        super.onCreate(savedInstanceState)
        requestPermission()
    }

    private fun requestPermission() {
//        @RequiresApi(Build.VERSION_CODES.R)
//        if (!Environment.isExternalStorageManager()) {
//            val managerExtensions: Intent = Intent()
//            managerExtensions.action = Manifest.permission.MANAGE_EXTERNAL_STORAGE
//        }
        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.ACCESS_COARSE_LOCATION)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .permission(Permission.READ_EXTERNAL_STORAGE)
            .permission(Permission.WRITE_EXTERNAL_STORAGE)
            .permission(Permission.REQUEST_INSTALL_PACKAGES)
            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, all: Boolean) {
//                    if (all) {
//                        Toast.makeText(this@MainActivity, "获取所有权限成功", Toast.LENGTH_SHORT).show()
//                    } else {
//                        Toast.makeText(this@MainActivity, "获取权限成功：$permissions", Toast.LENGTH_SHORT)
//                            .show()
//                    }
                    if (permissions.contains(Permission.READ_EXTERNAL_STORAGE)) {

//                        setContentView(R.layout.activity_main)
//                        MapManager.instance.init(mMapView)
//                        NIFragmentManager.instance.init(
//                            this@MainActivity,
//                            R.id.activity_main_menu_fragment,
//                            bottomSheetBehavior
//                        );
//                        initBehavior()
//                        initView()

//                        initDB();
                    }
                }

                override fun onDenied(permissions: MutableList<String>, never: Boolean) {
                    if (never) {
                        val dialogBuilder = MaterialAlertDialogBuilder(this@FlutterMainActivity)
                            .setTitle("提醒")
                            .setMessage("您拒绝了$permissions 等权限，点击确定在设置中手动授权！")
                            .create()


                        dialogBuilder.setButton(
                            DialogInterface.BUTTON_POSITIVE,
                            "确定"
                        ) { _, _ -> // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(
                                this@FlutterMainActivity,
                                permissions
                            )
                        }
                        dialogBuilder.setButton(
                            DialogInterface.BUTTON_NEGATIVE,
                            "取消",
                            Message()
                        )
                        dialogBuilder.show()
                    } else {
                        Toast.makeText(
                            this@FlutterMainActivity,
                            "拒绝授予权限：$permissions",
                            Toast.LENGTH_SHORT
                        )
                            .show()
                    }
                }
            })
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }


//    fun initDB() {
//        object : Thread() {
//            override fun run() {
//                //RealmUtils.getInstance().init(this@FlutterMainActivity, "realmTest")
//
//                db = MapLifeDataBase.getDatabase(
//                    this@FlutterMainActivity,
//                    NILayerManager.defaultDir + "/coremap.db"
//                )
//                db.elementDao.findList(0,1)
//            }
//        }.start()
//
//    }
}